package com.sg.zhui.visitor.utils;

import android.content.Context;
import android.graphics.Bitmap;


import com.core.lib.utils.main.ImageUtilBase;
import com.core.lib.utils.main.SdUtilBase;
import com.core.lib.utils.main.StringUtilBase;
import com.sg.zhui.visitor.R;

import java.io.File;


/**
 * Created by Administrator on 2016/11/1.
 */
public class SdLocal {
    public static final String LEPU_FOLDER = "/shougang/Kinder";
    public static String SHOUGANG_PATH = "";

    public static String SHOUGANG_PATH_SD = "";
    public static String DEFAULT_USER = "-1";

    /**
     * 外置存储
     *
     * @param context
     * @return
     */
    public static String getShouGangRootPathSD(Context context) {
        if (StringUtilBase.stringIsEmpty(SHOUGANG_PATH_SD)) {
            String path = SdUtilBase.getStorgePath(context);
            if (!StringUtilBase.stringIsEmpty(path)) {
                SHOUGANG_PATH_SD = path + LEPU_FOLDER;
            }
        }
        return SHOUGANG_PATH_SD;
    }

    // ==================================================================================

    /**
     * 内置app存储
     *
     * @param context
     * @return
     */
    private static String getLepuRootPath(Context context) {
        if (StringUtilBase.stringIsEmpty(SHOUGANG_PATH)) {
            String path = context.getFilesDir().getAbsolutePath();
            // String path = SdUtilBase.getStorgePath(context);
            if (!StringUtilBase.stringIsEmpty(path)) {
                SHOUGANG_PATH = path + LEPU_FOLDER;
            }

            //空间不足时，
//			try {
//				if(!SdUtilBase.checkIfFreeSpace(SHOUGANG_PATH)) {
//					SHOUGANG_PATH = getShouGangRootPathSD(context);
//				}
//			}catch (Exception e){
//			}
        }
        return SHOUGANG_PATH;
    }

    // ===================================================================
    // all user
    // 获取download folder
    private static String getDownloadFolder(Context context) {
        String fileFolder = StringUtilBase.combinePath(
                getShouGangRootPathSD(context), "Download");

        File file = new File(fileFolder);
        if (!file.exists()) {
            file.mkdirs();
        }
        return fileFolder;
    }

    // 获取temp folder
    public static String getTempFolder(Context context) {
        String fileFolder = StringUtilBase.combinePath(
                getShouGangRootPathSD(context), "Temp");

        File file = new File(fileFolder);
        if (!file.exists()) {
            file.mkdirs();
        }
        return fileFolder;
    }

    // 获取头像 缓存folder
    public static String getHeadIconFolder(Context context) {
        String fileFolder = StringUtilBase.combinePath(
                getShouGangRootPathSD(context), "HeadIcon");

        File file = new File(fileFolder);
        if (!file.exists()) {
            file.mkdirs();
        }
        return fileFolder;
    }

    private static String getCacheImageFolder(Context context) {
        String iconFolder = StringUtilBase.combinePath(
                getLepuRootPath(context), "/CacheImage");

        File file = new File(iconFolder);
        if (!file.exists()) {
            file.mkdirs();
        }
        return iconFolder;
    }

    private static String getCacheImageFolder(Context context, String userId) {
        String iconFolder = StringUtilBase.combinePath(
                getLepuRootPath(context), userId + "/CacheImage");

        File file = new File(iconFolder);
        if (!file.exists()) {
            file.mkdirs();
        }
        return iconFolder;
    }

    private static String getCacheDataFolder(Context context) {
        String fileFolder = StringUtilBase.combinePath(
                getShouGangRootPathSD(context), "/CacheData");

        File file = new File(fileFolder);
        if (!file.exists()) {
            file.mkdirs();
        }
        return fileFolder;
    }

    private static String getCacheDataFolder(Context context, String userId) {
        String fileFolder = StringUtilBase.combinePath(
                getShouGangRootPathSD(context), userId + "/CacheData");

        File file = new File(fileFolder);
        if (!file.exists()) {
            file.mkdirs();
        }
        return fileFolder;
    }

    public static String getChatImageFolder(Context context, String UserID) {
        String fileFolder = StringUtilBase.combinePath(
                getChatFolder(context), UserID + "/Image");

        File file = new File(fileFolder);
        if (!file.exists()) {
            file.mkdirs();
        }
        return fileFolder;
    }

    public static String getChatVoiceFolder(Context context, String UserID) {
        String fileFolder = StringUtilBase.combinePath(
                getChatFolder(context), UserID + "/Voice");

        File file = new File(fileFolder);
        if (!file.exists()) {
            file.mkdirs();
        }
        return fileFolder;
    }

    public static String getChatVideoFolder(Context context, String UserID) {
        String fileFolder = StringUtilBase.combinePath(
                getChatFolder(context), UserID + "/Video");

        File file = new File(fileFolder);
        if (!file.exists()) {
            file.mkdirs();
        }
        return fileFolder;
    }

    public static String getChatFolder(Context context) {
        String fileFolder = StringUtilBase.combinePath(
                getShouGangRootPathSD(context), "/Media");

        File file = new File(fileFolder);
        if (!file.exists()) {
            file.mkdirs();
        }
        return fileFolder;
    }

    // =====================================================================================
    // 术后天天知文件夹
    public static String getCacheDataSHTTZFolder(Context context) {
        String fileFolder = getCacheDataFolder(context) + "/shuhou";

        File file = new File(fileFolder);
        if (!file.exists()) {
            file.mkdirs();
        }
        return fileFolder;
    }

    public static String getCacheImagePath(Context context, String userId,
                                           String flag) {
        return StringUtilBase.combinePath(getCacheImageFolder(context, userId),
                flag + ".jpg");
    }

    public static String getCacheImagePath(Context context, String flag) {
        return StringUtilBase.combinePath(getCacheImageFolder(context), flag
                + ".jpg");
    }

    /**
     * 获取用户的头像
     *
     * @param context
     * @param userId
     * @return
     */
    public static String getUserPath(Context context, String userId) {
        return StringUtilBase.combinePath(getTempFolder(context), userId
                + ".jpg");
    }

    public static String getAuthFirstPath(Context context) {
        return StringUtilBase.combinePath(getTempFolder(context), "auth_first.jpg");
    }

    public static String getAuthSecondPath(Context context) {
        return StringUtilBase.combinePath(getTempFolder(context), "auth_second.jpg");
    }


    public static String getAppIconPath(Context context) {
        String appIconPath = StringUtilBase.combinePath(getTempFolder(context),
                "app_icon" + ".png");
        if (!new File(appIconPath).exists()) {
            Bitmap iconBitmap = ImageUtilBase.getBitmapByResId(context,
                    R.drawable.app_icon);
            ImageUtilBase.saveImageToSd(Bitmap.CompressFormat.PNG, iconBitmap,
                    appIconPath);
        }
        return appIconPath;
    }

    public static String getTempPhotoPath(Context context, String flag) {
        return StringUtilBase
                .combinePath(getTempFolder(context), flag + ".jpg");
    }

    public static String getTempPhotoPath(Context context) {
        return StringUtilBase.combinePath(getTempFolder(context), "temp.jpg");
    }

    public static String getTempFilePath(Context context, String flag) {
        return StringUtilBase
                .combinePath(getTempFolder(context), flag + ".txt");
    }

    public static String getDebugLogPath(Context context) {
        return StringUtilBase.combinePath(getTempFolder(context), "debug.log");
    }

    public static String getDatabasePath(Context context, String dbName) {
        return context.getDatabasePath(dbName).getPath();
    }

    public static String getDownloadApkPath(Context context, String name) {
        return StringUtilBase.combinePath(getDownloadFolder(context), name
                + ".apk");
    }

    public static String getDownloadZipPath(Context context, String name) {
        return StringUtilBase.combinePath(getDownloadFolder(context), name
                + ".zip");
    }

    public static String getShuHouTianTianZhiPath(Context context, String name) {
        return StringUtilBase.combinePath(getCacheDataSHTTZFolder(context),
                name + ".zip");
    }

    // 读SD中的文件
//    public static String readFileSdcardFile(String fileName) throws IOException {
//        String res = "";
//        try {
//            FileInputStream fin = new FileInputStream(fileName);
//            int length = fin.available();
//            byte[] buffer = new byte[length];
//            fin.read(buffer);
//            res = EncodingUtils.getString(buffer, "UTF-8");
//            fin.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return res;
//    }

    public static String getChatImagePath(Context context,
                                          String name) {
        return StringUtilBase.combinePath(
                getChatImageFolder(context,DEFAULT_USER), name + ".jpg");
    }

    public static String getChatImagePath(Context context, String userId,
                                          String name) {
        return StringUtilBase.combinePath(
                getChatImageFolder(context, userId), name + ".jpg");
    }

    public static String getChatVoicePath(Context context, String userId,
                                          String name) {
        return StringUtilBase.combinePath(
                getChatVoiceFolder(context, userId), name + ".amr");
    }

    public static String getChatVideoPath(Context context, String userId,
                                          String name) {
        return StringUtilBase.combinePath(
                getChatVideoFolder(context, userId), name + ".mp4");
    }

    public static String getChatVideoPathCompress(String fullFileName) {
        return fullFileName + "_compress.mp4";
    }

    public static void createHideFilePath(Context context) {
        String filePath = SdLocal.getShouGangRootPathSD(context);
        File nomedia = new File(filePath + "/.nomedia");

        try {
            if (!nomedia.exists()) {
                nomedia.createNewFile();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
